package example.com.zyfypt_618zgm.activity;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ImageButton;
import android.widget.LinearLayout;

import java.util.ArrayList;
import java.util.List;

import example.com.zyfypt_618zgm.R;
import example.com.zyfypt_618zgm.fragment.CollectFragment;
import example.com.zyfypt_618zgm.fragment.MyFragment;

/**
 * Created by 小茗同学 on 2017/6/1.
 */

public class CollectMain extends AppCompatActivity {
    private LinearLayout layout1,layout2,layout3,layout4,layout5;
    private ImageButton ib1,ib2,ib3,ib4,ib5;
    private ViewPager vp;
    private List<Fragment> listfragment;
    private FragmentPagerAdapter fpadapter;
    public static int index1=0;
    public static String truemod1;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.focus);
        init();
        initEvents();
        initFragment();
        selectTab(0);
        index1=0;
    }

    private void init() {
        layout1=(LinearLayout)findViewById(R.id.layout6);
        layout2=(LinearLayout)findViewById(R.id.layout7);
        layout3=(LinearLayout)findViewById(R.id.layout8);
        layout4=(LinearLayout)findViewById(R.id.layout9);
        layout5=(LinearLayout)findViewById(R.id.layout10);
        ib1=(ImageButton)findViewById(R.id.imageButton6);
        ib2=(ImageButton)findViewById(R.id.imageButton7);
        ib3=(ImageButton)findViewById(R.id.imageButton8);
        ib4=(ImageButton)findViewById(R.id.imageButton9);
        ib5=(ImageButton)findViewById(R.id.imageButton10);
        vp=(ViewPager)findViewById(R.id.viewpager);
    }
    View.OnClickListener onClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            //先将四个按钮都设成未选图片
            resetImage();
            switch (view.getId()) {
                case R.id.layout6:
                    selectTab(0);
                    index1=0;
                    break;
                case R.id.layout7:
                    selectTab(1);
                    index1=1;
                    break;
                case R.id.layout8:
                    selectTab(2);
                    index1=2;
                    break;
                case R.id.layout9:
                    selectTab(3);
                    index1=3;
                    break;
                case R.id.layout10:
                    selectTab(4);
                    index1=4;
                    break;
            }
        }
    };
    private void initEvents(){
        layout1.setOnClickListener(onClickListener);
        layout2.setOnClickListener(onClickListener);
        layout3.setOnClickListener(onClickListener);
        layout4.setOnClickListener(onClickListener);
        layout5.setOnClickListener(onClickListener);
    }
    private  void resetImage(){
        ib1.setImageResource(R.drawable.ziyuan);
        ib2.setImageResource(R.drawable.kejian);
        ib3.setImageResource(R.drawable.shipin);
        ib4.setImageResource(R.drawable.anli);
        ib5.setImageResource(R.drawable.shezhi);
    }
    private void selectTab(int i) {
        switch (i){
            case 0:ib1.setImageResource(R.drawable.ziyuan2);
                break;
            case 1:ib2.setImageResource(R.drawable.kejian2);
                break;
            case 2:ib3.setImageResource(R.drawable.shipin2);
                break;
            case 3:ib4.setImageResource(R.drawable.anli2);
                break;
            case 4:ib5.setImageResource(R.drawable.shezhi2);
                break;
        }
        vp.setCurrentItem(i);
    }
    private void initFragment() {
        listfragment = new ArrayList<Fragment>();
        listfragment.add(new CollectFragment("article"));
        listfragment.add(new CollectFragment("tware"));
        listfragment.add(new CollectFragment("video"));
        listfragment.add(new CollectFragment("tcase"));
        listfragment.add(new CollectFragment("project"));
        listfragment.add(new MyFragment());
        fpadapter= new  FragmentPagerAdapter(getSupportFragmentManager())  {

            @Override
            public int getCount() {
                return listfragment.size();
            }

            @Override
            public Fragment getItem(int position) {
                return listfragment.get(position);
            }
        };
        vp.setAdapter(fpadapter);
        vp.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                vp.setCurrentItem(position);
                resetImage();
                selectTab(position);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }
}
